package javaSenior.threadpoolLearn;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Lenl
 * @version v1.0
 * @create 2021-06-03 10:00
 * @description 创建线程的方式4：使用线程池
 *好处：
 *  1.提高响应速度
 *  2.降低资源消耗
 *  3.便于线程管理
 *
 * 这里讲的较少，还需要在实践中学习
 */
public class ThreadPool {
    public static void main(String[] args) {
        //提供指定线程数量的线程池
        ExecutorService service= Executors.newFixedThreadPool(10);
        //设置线程池的属性
        ThreadPoolExecutor service1=(ThreadPoolExecutor)service;
        service1.setCorePoolSize(15);
        //System.out.println(service.getClass());
        //执行指定线程的操作，需要实现Runnable或Callable接口的实现类实例
        service.execute(new NumberThread());//适合适用于Runnable
        service.submit(new NumberThread1());//适合适用于Callable
        service.shutdown();
    }
}

class NumberThread implements Runnable{
    @Override
    public void run() {
        for (int i=0;i<100;i++){
            if(i%2==0){
                System.out.println(Thread.currentThread().getName()+":"+ i);
            }
        }
    }
}
class NumberThread1 implements Runnable{
    @Override
    public void run() {
        for (int i=0;i<100;i++){
            if(i%2!=0){
                System.out.println(Thread.currentThread().getName()+":"+ i);
            }
        }
    }
}